<template>
	<div class="app-container">
		<el-form class="queryForms" :model="queryParams" ref="queryForm" size="small" :inline="true"
			v-show="showSearch">
			<el-form-item label="任务单号" prop="repairNumber">
				<el-input maxlength="20" show-word-limit v-model="queryParams.repairNumber" placeholder="请输入任务单号"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="客户名称" prop="clientName">
				<el-input maxlength="20" show-word-limit v-model="queryParams.clientName" placeholder="请输入客户名称"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="产品型号" prop="productModel">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productModel" placeholder="请输入产品型号"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="产品编号" prop="productNumber">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productNumber" placeholder="请输入产品编号"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="3B号" prop="bNumber">
				<el-input maxlength="20" show-word-limit v-model="queryParams.bNumber" placeholder="请输入3B号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="分配状态" prop="distributionStationStatus">
				<el-select v-model="queryParams.distributionStationStatus" placeholder="请选择">
					<el-option label="未分配" value="0"> </el-option>
					<el-option label="已分配" value="1"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="工段" prop="workshopSectionName">
				<el-input maxlength="20" show-word-limit v-model="queryParams.workshopSectionName" placeholder="请输入工段"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="故障核实审批状态" prop="malfunctionApprovalStatus" label-width="140px">
				<el-select v-model="queryParams.malfunctionApprovalStatus">
					<el-option v-for="item in ApprovalStatusList" :key="item.id" :label="item.name" :value="item.id">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" size="mini" :disabled="multiple" @click="handleApproves"
					v-hasPermi="['erp:task:taskAllocation']">批量分配</el-button>
			</el-col>
			<right-toolbar menuNames="taskAllocation71" @changeTableData="changeTableData" :showSearch.sync="showSearch"
				@queryTable="handleQuery" :columns.sync="columns" perm="erp:taskAllocation:list"></right-toolbar>
		</el-row>
		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''"
			class="informationTab" ref="eleTab">
			<el-table :key="treetablekey" @expand-change="handleExpandChange" @mousedown.native="mousedownTable"
				@row-click="row_click" @cell-mouse-enter="cell_mouse_enter" @cell-mouse-leave="cell_mouse_leave"
				@mouseup.native="mouseupTable" @mouseleave.native="mouseupTable" :row-class-name="row_class_name"
				ref="multipleTable" row-key="id" lazy :load="load" :default-expand-all="false"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }" v-loading="loading"
				:data="repairList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55">
					           <template slot-scope="scope">
						<div>{{ scope.row.sort }}</div>
						           
					</template>
				</el-table-column>

				<el-table-column label="返修任务单号" prop="repairNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[0].visible" />
				<el-table-column label="客户名称" prop="clientName" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[1].visible" />
				<el-table-column label="客户代表" prop="productRepresentativeName" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[2].visible" />
				<el-table-column label="产品名称" prop="productName" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[3].visible" />
				<el-table-column label="产品型号" prop="productModel" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[4].visible" />
				<el-table-column label="产品编号" prop="productNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[5].visible" />
				<el-table-column label="分配状态" align="center" prop="distributionStationStatus" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[6].visible">
					<template slot-scope="scope">
						<el-tag type="success" v-if="scope.row.distributionStationStatus == '1'">已分配</el-tag>
						<el-tag type="warning" v-if="scope.row.distributionStationStatus == '0'">未分配</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="工段" prop="workshopSectionName" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[7].visible" />
				<el-table-column label="3B号" prop="bNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[8].visible" />
				<el-table-column label="计划开始时间" prop="programBeginTime" min-width="180" :show-overflow-tooltip="true"
					v-if="columns[9].visible" />
				<el-table-column label="计划完成时间" prop="programFinishTime" min-width="180" :show-overflow-tooltip="true"
					v-if="columns[10].visible" />
				<el-table-column label="维修类别" prop="maintenanceCategoryName" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[11].visible" />
				<!-- <el-table-column label="故障原因" prop="failureCause" min-width="150" :show-overflow-tooltip="true" v-if="columns[10].visible" /> -->
				<el-table-column label="故障模式" prop="failureMode" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[12].visible" />
				<el-table-column label="故障核实情况" align="center" prop="failureRemark" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[13].visible"> </el-table-column>
				<el-table-column label="工艺通知单编号" prop="processNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[14].visible" />
				<el-table-column label="状态" prop="malfunctionStatus" min-width="150" :show-overflow-tooltip="true"
					align="center" v-if="columns[11].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.hasChildren">
							<div v-if="scope.row.repairMalfunctionStatus === '0'" style="color: #666">待核实</div>
							<div v-if="scope.row.repairMalfunctionStatus === '1'" style="color: #e1756d">待提交</div>
							<div v-if="scope.row.repairMalfunctionStatus === '2'" style="color: #63b0b3">进行中</div>
							<div v-if="scope.row.repairMalfunctionStatus === '3'" style="color: #7ebf27">已完成</div>
						</div>
						<div v-else>
							<el-tag v-if="scope.row.malfunctionStatus === '0'" style="color: #666">
								<dict-tag :options="dict.type.fault_status" :value="scope.row.malfunctionStatus" />
							</el-tag>
							<el-tag v-if="scope.row.malfunctionStatus === '1'" style="color: #e1756d">
								<dict-tag :options="dict.type.fault_status" :value="scope.row.malfunctionStatus" />
							</el-tag>
							<el-tag v-if="scope.row.malfunctionStatus === '2'" style="color: #63b0b3">
								<dict-tag :options="dict.type.fault_status" :value="scope.row.malfunctionStatus" />
							</el-tag>
							<el-tag v-if="scope.row.malfunctionStatus === '3'" style="color: #7ebf27">
								<dict-tag :options="dict.type.fault_status" :value="scope.row.malfunctionStatus" />
							</el-tag>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="故障核实审批状态" prop="malfunctionApprovalStatus" min-width="150" align="center"
					:show-overflow-tooltip="true" v-if="columns[13].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.hasChildren">
							<div v-if="scope.row.malfunctionApprovalStatus == 1" style="color: red">需审批</div>
							<div v-if="scope.row.malfunctionApprovalStatus == 2" style="color: green">已完成</div>
						</div>
						<div v-else>
							<el-tag type="info" v-if="scope.row.malfunctionApprovalStatus === '0'" class="disableClick">
								<dict-tag :options="dict.type.default_approve"
									:value="scope.row.malfunctionApprovalStatus" />
							</el-tag>
							<el-tag v-if="scope.row.malfunctionApprovalStatus === '1'"
								@click="showApproveList(scope.row)" class="clickable">
								<dict-tag :options="dict.type.default_approve"
									:value="scope.row.malfunctionApprovalStatus" />
							</el-tag>
							<el-tag type="success" v-if="scope.row.malfunctionApprovalStatus === '2'"
								@click="showApproveList(scope.row)" class="clickable">
								<dict-tag :options="dict.type.default_approve"
									:value="scope.row.malfunctionApprovalStatus" />
							</el-tag>
							<el-tag type="danger" v-if="scope.row.malfunctionApprovalStatus === '3'"
								@click="showApproveList(scope.row)" class="clickable">
								<dict-tag :options="dict.type.default_approve"
									:value="scope.row.malfunctionApprovalStatus" />
							</el-tag>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="操作" class-name="small-padding fixed-width" fixed="right" width="200"
					align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-wallet" @click="handleFile(scope.row)"
							v-hasPermi="['erp:task:files']" v-if="scope.row.hasChildren">附件</el-button>
						<el-button size="mini" type="text" icon="el-icon-s-grid" @click="handleApprove(scope.row)"
							v-hasPermi="['erp:task:taskAllocation']" v-if="!scope.row.hasChildren">分配工段</el-button>
						<el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)"
							v-hasPermi="['erp:task:details']" v-if="scope.row.hasChildren">详情</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<pagination menuNames="Pagination74" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize" @pagination="getList" />
		<!-- 添加或修改返修单对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" :model="form" :rules="rules" label-width="100px">
				<el-row :gutter="20" v-if="singleData">
					<el-col :span="12">
						<el-form-item label="返修任务单" prop="repairNumber">
							<el-input disabled v-model="form.repairNumber" placeholder="请输入返修任务单" maxlength="20"
								show-word-limit />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="客户名称" prop="clientName">
							<el-input disabled v-model="form.clientName" placeholder="请输入客户名称" maxlength="20"
								show-word-limit />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="客户代表" prop="productRepresentativeName">
							<el-input disabled v-model="form.productRepresentativeName" placeholder="请输入客户代表"
								maxlength="20" show-word-limit />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="产品名称" prop="productName">
							<el-input disabled v-model="form.productName" placeholder="请输入产品名称" maxlength="20"
								show-word-limit />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="产品型号" prop="productModel">
							<el-input disabled v-model="form.productModel" placeholder="请输入产品型号" maxlength="20"
								show-word-limit />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="产品编号" prop="productNumber">
							<el-input disabled v-model="form.productNumber" placeholder="请输入产品编号" maxlength="20"
								show-word-limit />
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="计划开始时间" prop="programBeginTime">
							<el-input disabled v-model="form.programBeginTime" placeholder="请输入计划开始时间" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="计划完成时间" prop="programFinishTime">
							<el-input disabled v-model="form.programFinishTime" placeholder="请输入计划完成时间" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="3B号" prop="bNumber">
							<el-input disabled v-model="form.bNumber" placeholder="请输入3B号" maxlength="20"
								show-word-limit />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="工时" prop="manHour">
							<el-input disabled v-model="form.manHour" placeholder="请输入工时" maxlength="20"
								show-word-limit />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="工段" prop="workshopSectionId">
							<el-select v-model="form.workshopSectionId" @change="selectSection">
								<el-option v-for="item in sectionlist" :key="item.id" :value="item.id"
									:label="item.workshopSectionName"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
		<el-dialog title="返修任务详情" :visible.sync="openDetail" width="900px" append-to-body v-dialogDrag>
			<RepairInfo ref="repairdetail" />
		</el-dialog>
	</div>
</template>

<script>
import { listRepairProduct, distributionStation, repairProductDetails, batchDistributionStation } from "@/api/repair/repairProduct"
import { listRepair } from "@/api/repair/repair"
import { clientList, workshopSectionlist, getcolumn } from "@/api/selectList"
import { getClient } from "@/api/system/custom"
import RepairInfo from "@/views/components/RepairInfo"
import { mapGetters } from "vuex"
import searchStateMixin from '@/layout/mixin/searchStateMixin.js'
export default {
	name: "TaskAllocation_menu",
	dicts: ["default_approve", "fault_status", "maintain_cat"],
	components: { RepairInfo },
	computed: {
		...mapGetters(["sidebar"])
	},
	mixins: [searchStateMixin],
	data() {
		return {
			ApprovalStatusList: [
				{
					id: "0",
					name: "未提交"
				},
				{
					id: "1",
					name: "审批中"
				},
				{
					id: "2",
					name: "已通过"
				},
				{
					id: "3",
					name: "未通过"
				}
			],
			tableTreeRefreshTool: {}, // 用于存储 resolve 方法和展开状态
			isMousedownTable: false, //是否按下表格
			currentEnterRow: null, //当前移入的行数据
			rows: [],
			eleTabTop: 0, //表格容器距浏览器顶部距离
			treetablekey: Math.random(),
			// 工段列表
			sectionlist: [],
			openDetail: false,
			allocationId: "",
			repairProductList: [],
			maps: new Map(),
			// 监管协议号
			tclientProtocol: [],
			// 客户列表
			clientDatas: [],
			// 是否展开，默认全部展开
			isExpandAll: true,
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
		showSearch: true,
			// 总条数
			total: 0,
			// 返修单表格数据
			repairList: [],
			// 返修单产品列表格数据
			tRepairProductList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				repairNumber: null,
				clientId: null,
				clientName: null,
				productRepresentativeId: null,
				productRepresentativeName: null,
				contractNumber: null,
				protocolId: null,
				protocolNumber: null,
				receptionTime: null,
				failureCause: null,
				bNumber: null,
				programBeginTime: null,
				programFinishTime: null,
				retirementRepairlStatus: "0",
				repairApprovalStatus: "2",
				approvalStatus: null,
				distributionStationStatus:  "0",
				productModel: null,
				productNumber: null,
				workshopSectionName: null,
				malfunctionApprovalStatus: "2"
			},
			// 表单参数
			form: {},
			repairId: "",
			// 表单校验
			rules: {
				workshopSectionId: [{ required: true, message: "工段不能为空", trigger: "blur" }]
			},
			singleData: true,
			// 列信息
			columns: [
				{ key: 0, label: `返修任务单号`, visible: true },
				{ key: 1, label: `客户名称`, visible: true },
				{ key: 2, label: `客户代表`, visible: true },
				{ key: 3, label: `产品名称`, visible: true },
				{ key: 4, label: `产品型号`, visible: true },
				{ key: 5, label: `产品编号`, visible: true },
				{ key: 6, label: `分配状态`, visible: true },
				{ key: 7, label: `工段`, visible: true },
				{ key: 8, label: `3B号`, visible: true },
				{ key: 9, label: `计划开始时间`, visible: true },
				{ key: 10, label: `计划完成时间`, visible: true },
				{ key: 11, label: `维修类别`, visible: true },
				// { key: 10, label: `故障原因`, visible: true },
				{ key: 12, label: `故障模式`, visible: true },
				{ key: 13, label: `故障核实情况`, visible: true },
				{ key: 14, label: `工艺通知单编号`, visible: true }
			]
		}
	},
	created() {
		// 如果是页面刷新，重置查询
		if (window.performance && performance.navigation.type === performance.navigation.TYPE_RELOAD) {
			this.resetForm("queryForm")
			// 清除URL中的查询参数（不刷新页面）
			window.history.replaceState({}, document.title, window.location.pathname);
		}
	},
	watch: {
		"$route.query": {
			handler(newQuery, oldQuery) {
				if (newQuery.queryData) {
					if (Object.keys(newQuery.queryData).length === 0) {
						this.resetQuerys()
					} else {
						this.queryParams = { ...this.queryParams, ...JSON.parse(newQuery.queryData) }
            this.queryParams.malfunctionApprovalStatus = "2"
					}
				} this.getSize()
			},
			immediate: true, // 立即执行一次
			deep: true // 深度监听
		}
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		// this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
		getSize() {
			getcolumn({ pageName: "Pagination74" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		handleFile(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id,
					typeId: 13,
					perm: "erp:task:files",
					// queryParams: JSON.stringify(this.queryParams),
					paths: "/taskAllocation/taskAllocation_menu"
				}
			})
		},
		resetworkshopSection() {
			this.form = {
				workshopSectionId: undefined,
				workshopSectionName: undefined
			}
		},
		handleApproves() {
			this.singleData = false
			let flag = true
			this.resetworkshopSection()
			this.ids.forEach(item => {
				item += ""
				if (!item.includes("c")) {
					this.$message.error("只能给子级分配工段")
					flag = false
				}
			})
			if (flag) {
				this.getworkshopSection()
			}
		},
		cancel() {
			this.open = false
			this.$refs.multipleTable.clearSelection()
		},
		// 分配
		handleApprove(row) {
			console.log(row)
			this.reset()
			this.allocationId = row.id.replace("c", "")
			this.repairId = row.repairId
			this.singleData = true
			if (row.distributionStationStatus == "1") {
				this.$confirm("该产品已经分配过工段，是否确认再次分配?", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				})
					.then(() => {
						this.hasApprove(row.id.replace("c", ""))
					})
					.catch(() => {
						this.$message({
							type: "info",
							message: "已取消分配"
						})
					})
			} else {
				this.hasApprove(this.allocationId)
			}
		},
		hasApprove(id) {
			this.getrepairProductDetails(id)
			this.getworkshopSection()
		},
		getrepairProductDetails(id) {
			repairProductDetails(id).then(res => {
				this.form = res.data
			})
		},
		getworkshopSection() {
			workshopSectionlist({ state: "1" }).then(res => {
				this.sectionlist = res.rows
				this.title = "分配工段"
				this.open = true
			})
		},
		// 展开/收起时触发
		handleExpandChange(row, expanded) {
			const curr = this.tableTreeRefreshTool[row.id]
			if (!curr) return
			curr.expandCount++
			if (expanded && curr.expandCount > 1 && !curr.prevStatus) {
				listRepairProduct({
					repairId: row.id,
					approvalStatus: "2",
					...this.queryParams
				}).then(res => {
					res.rows.forEach((item, index) => {
						// 父子id会冲突
						item.id = "c" + item.id
						item.sort = index + 1
					})
					curr.resolve(res.rows)
				})
			}
			curr.prevStatus = expanded
		},
		//懒加载
		async load(tree, treeNode, resolve) {
			if (!this.maps.has(tree.id)) {
				this.maps.set(tree.id, { tree: tree, treeNode, resolve })
			}
			this.tableTreeRefreshTool[tree.id] = {
				resolve,
				expandCount: 0, // 展开次数
				prevStatus: false // 上一次的展开状态
			}
			let queryParams = JSON.parse(JSON.stringify(this.queryParams))
			delete queryParams.pageSize
			delete queryParams.pageNum
			let result = await listRepairProduct({
				repairId: tree.id,
				approvalStatus: "2",
				...queryParams
			})
			if (result.rows.length) {
				result.rows.forEach((item, index) => {
					item.id = "c" + item.id
					item.sort = index + 1
				})
				resolve(result.rows)
			} else {
				resolve([])
			}
		},
		handleDetail(row) {
			this.title = "查看详情"
			this.openDetail = true
			this.$nextTick(() => {
				this.$refs.repairdetail.resetFormData(row.id)
			})
		},
		selectSection(e) {
			this.sectionlist.forEach(item => {
				if (e == item.id) {
					this.form.workshopSectionName = item.workshopSectionName
				}
			})
		},
		getClientData(e) {
			getClient(e).then(response => {
				if (response.data.tclientProtocolList && response.data.tclientProtocolList.length > 0) {
					this.tclientProtocol = response.data.tclientProtocolList.filter(item => item.state == "1")
					if (this.tclientProtocol.length == 1) {
						this.form.protocolId = response.data.tclientProtocolList[0].id
						this.form.protocolNumber = response.data.tclientProtocolList[0].protocolNumber
					}
				}
			})
		},
		/** 查询返修单列表 */
		getList() {
			this.loading = true
			listRepair(this.queryParams).then(response => {
				this.repairList = response.rows
				this.repairList.forEach((item, index) => {
					item.hasChildren = true //树结构hasChildren
					item.sort = index + 1
				})
				this.total = response.total
				this.loading = false
			})
		},
		refreshLoadTree(parentId) {
			const { tree, treeNode, resolve } = this.maps.get(parentId)
			this.load(tree, treeNode, resolve) // 重新加载子级数据
			this.$refs.multipleTable.clearSelection()
		},
		// 关闭所有展开的行
		closeAllExpansions() {
			this.repairList.forEach(row => {
				this.$refs.multipleTable.toggleRowExpansion(row, false)
			})
			this.$refs.multipleTable.clearSelection()
			this.getList()
		},

		// 表单重置
		reset() {
			this.form = {
				id: null,
				repairNumber: null,
				clientId: null,
				clientName: null,
				productRepresentativeId: null,
				productRepresentativeName: null,
				contractNumber: null,
				protocolId: null,
				protocolNumber: null,
				receptionTime: null,
				failureCause: null,
				bNumber: null,
				programBeginTime: null,
				programFinishTime: null,
				repairApprovalStatus: null,
				remark: null,
				distributionStationStatus:  "0",

			}
			this.tRepairProductList = []
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.treetablekey = Math.random()
			this.queryParams.pageNum = 1
			this.saveSearchState()
			this.closeAllExpansions()
		},
		resetQuerys() {
			; (this.queryParams = {
				pageNum: 1,
				pageSize: this.queryParams.pageSize,
				repairNumber: null,
				clientId: null,
				clientName: null,
				productRepresentativeId: null,
				productRepresentativeName: null,
				contractNumber: null,
				protocolId: null,
				protocolNumber: null,
				receptionTime: null,
				failureCause: null,
				bNumber: null,
				programBeginTime: null,
				programFinishTime: null,
				retirementRepairlStatus: "0",
				repairApprovalStatus: "2",
				approvalStatus: null,
				distributionStationStatus:  "0",
				productModel: null,
				productNumber: null,
				workshopSectionName: null,
				malfunctionApprovalStatus: "2"
			}),
				this.resetForm("queryForm")
		},
		/** 重置按钮操作 */
		resetQuery() {
			; (this.queryParams = {
				pageNum: 1,
				pageSize: this.queryParams.pageSize,
				repairNumber: null,
				clientId: null,
				clientName: null,
				productRepresentativeId: null,
				productRepresentativeName: null,
				contractNumber: null,
				protocolId: null,
				protocolNumber: null,
				receptionTime: null,
				failureCause: null,
				bNumber: null,
				programBeginTime: null,
				programFinishTime: null,
				retirementRepairlStatus: "0",
				repairApprovalStatus: "2",
				approvalStatus: null,
				distributionStationStatus:  "0",
				productModel: null,
				productNumber: null,
				workshopSectionName: null,
				malfunctionApprovalStatus: "2"
			}),
				this.resetForm("queryForm")
				this.clearSearchCache()
			this.handleQuery()
		},
		// 表格按下
		mousedownTable(event) {
			const target = event.target
			// 判断target的class属性确定是否点击在复选框上
			if (target.classList.contains("el-checkbox__inner")) {
				this.currentEnterRow && this.$refs.multipleTable.toggleRowSelection(this.currentEnterRow)
				this.isMousedownTable = true
			}
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.multipleTable.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.multipleTable.doLayout()
			})
		},

		// 进入单元格
		cell_mouse_enter(row, column, cell, event) {
			this.isMousedownTable && this.$refs.multipleTable.toggleRowSelection(row)
			this.currentEnterRow = row
		},
		// 离开单元格
		cell_mouse_leave(row, column, cell, event) {
			this.currentEnterRow = null
		},
		// 鼠标弹起或者离开表格
		mouseupTable(d) {
			this.isMousedownTable = false
		},
		// 表格每行样式
		row_class_name({ row, rowIndex }) {
			return this.rows.find(v => v.id == row.id) ? "selected" : ""
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.rows = selection
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		getclientList() {
			clientList({
				clientState: "1"
			}).then(res => {
				this.clientDatas = res.rows
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					const loading = this.$loading({
						lock: true,
						text: "Loading",
						spinner: "el-icon-loading",
						background: "rgba(0, 0, 0, 0.7)"
					})
					if (this.singleData) {
						this.form.id = this.allocationId
						console.log(this.form.id)
						this.form.distributionStationStatus = 1
						distributionStation(this.form)
							.then(response => {
								this.$modal.msgSuccess("分配成功")
								loading.close()
								this.open = false
								this.refreshLoadTree(this.repairId) // 刷新子级数据
								this.$refs.multipleTable.clearSelection()
								localStorage.setItem("todoMessage", "taskAllocation1")
							})
							.catch(() => {
								loading.close()
							})
							.finally(() => {
								loading.close()
							})
					} else {
						let copyIds = []
						this.ids.forEach(item => {
							copyIds.push(item.replace("c", ""))
						})
						let forms = {
							ids: copyIds,
							workshopSectionId: this.form.workshopSectionId,
							workshopSectionName: this.form.workshopSectionName
						}
						batchDistributionStation(forms)
							.then(res => {
								this.$modal.msgSuccess("分配成功")
								loading.close()
								this.open = false
								this.closeAllExpansions()
								this.$refs.multipleTable.clearSelection()
								localStorage.setItem("todoMessage", "taskAllocation2")
							})
							.catch(() => {
								loading.close()
							})
							.finally(() => {
								loading.close()
							})
					}
				}
			})
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				"erp/repair/export",
				{
					...this.queryParams
				},
				`repair_${new Date().getTime()}.xlsx`
			)
		}
	}
}
</script>
